home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 205_01 / white.c < prev    next >
Text File  |  1980-01-01  |  1KB  |  50 lines

  1. /*
  2. HEADER:                 CUG205.00;
  3. TITLE:                  Whitespace Counter;
  4. DATE:                   09/24/86;
  5. DESCRIPTION:
  6.   "Counts the blanks, tabs, and carriage returns in a file.";
  7. KEYWORDS:               Software tools, Text filters, white space counter;
  8. SYSTEM:                 MS-DOS;
  9. FILENAME:               WHITE.C;
  10. WARNINGS:
  11.   "The author claims copyrights and authorizes non-commercial use only.";
  12. AUTHORS:                 Michael M. Yokoyama;
  13. COMPILERS:              Microsoft;
  14. */
  15.  
  16. #include <stdio.h>
  17.  
  18. main(argc,argv)
  19. int argc;               /* Number of command line words     */
  20. char *argv[];           /* Pointers to command line words   */
  21. {
  22.   FILE *in;             /* File used for input              */
  23.   int c, nb, nt, nl;    /* Number of blanks, tabs, newlines */
  24.  
  25.   nb = nt = nl = 0;
  26.  
  27.   if (argc != 2) {
  28.     fprintf(stderr,"Whitespace counting utility\n");
  29.     fprintf(stderr,"Usage:  white source\n");
  30.     exit(1);
  31.   }
  32.  
  33.   if ((in = fopen(argv[1],"r")) == NULL ) {
  34.     fprintf(stderr,"Can't open source file: %s\n",argv[1]);     
  35.     exit(2);
  36.   }
  37.  
  38.   while ((c = getc(in)) != EOF) {
  39.     if (c == ' ')
  40.       ++nb;
  41.     else if (c == '\t')
  42.       ++nt;
  43.     else if (c == '\n')
  44.       ++nl;
  45.   }
  46.   fclose(in);
  47.   printf("Spaces    Tabs    Newlines\n");
  48.   printf("%6d  %6d  %6d\n", nb, nt, nl);
  49. }
  50.